package cn.itcast.order.controller;


import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.entity.TbUser;
import cn.itcast.order.entity.TbOrder;
import cn.itcast.order.entity.VO.Order;
import cn.itcast.order.service.TbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class TbOrderController {

    @Autowired
    private TbOrderService orderService;

    @Autowired
    private RestTemplate restTemplate; // 用于不同服务之间来发送请求

    @Autowired
    private UserClient userClient;


    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId, @RequestParam("token") String token) {
        // 根据id查询订单并返回
        TbOrder tbOrder = orderService.getById(orderId);
        // 利用用户id查询用户信息 [RestTemplate]
//        String url = "http://localhost:8081/user/" + tbOrder.getUserId();
//        String url = "http://userservice/user/" + tbOrder.getUserId();
//        // get 请求         请求路径, 返回值对象
//        TbUser user = restTemplate.getForObject(url, TbUser.class);


        // Feign 远程调用
        TbUser user = userClient.findById(tbOrder.getUserId(), token);


        Order order = new Order();
        order.setOrder(tbOrder);
        order.setUser(user);

        return order;
    }
}
